package com.crm.controller.before_sale;

import com.crm.excel.export.ExportExcel;
import com.crm.pojo.ClientInfo;
import com.crm.pojo.SaleChance;
import com.crm.service.before_sale.SaleChanceService;
import com.crm.vo.Result;
import com.crm.vo.SaleChanceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SaleChanceController {
    @Autowired
    private SaleChanceService saleChanceService;
    //销售机会表
    @GetMapping("zjk-saleChance")
    public String tosaleChanceDatagrid(){ return "before-sale/saleChance";}
    //销售机会表单
    @GetMapping("zjk-saleChance-form")
    public String tosaleChanceFrom(){
        return "before-sale/saleChance-form";
    }
    //销售机会详情表单（仅查看）
    @GetMapping("zjk-saleChance-details")
    public String tosaleChanceDetails(){
        return "before-sale/saleChance-details";
    }
    @GetMapping("zjk-advance-form")
    public String toAdvanceForm(){
        return "before-sale/advance-form";
    }
    @GetMapping("zjk-edit-saleChance-form")
    public String toEditSaleChanceForm(){
        return "before-sale/saleChance-edit-form";
    }

    /**
     * 分页展示销售机会列表
     * @param page
     * @param rows
     * @return
     */
    @PostMapping("getSaleChanceList")
    @ResponseBody
    public Map querySaleChanceList(int page,int rows){
        List<SaleChanceVO> list = saleChanceService.queryAllSaleChanceList(page,rows);
        Long total = saleChanceService.getSaleChanceExistCount();
        Map map = new HashMap();
        map.put("rows",list);
        map.put("total",total);
        return map;
    }

    /**
     * 删除销售机会
     * @param chId
     * @return
     */
    @PostMapping("deleteSaleChance")
    @ResponseBody
    public Result deleteSaleChanceById(String chId){
        Long id = chId ==null ? 0 : new Long(chId);
        return saleChanceService.deleteSaleChanceById(id);

    }

    /**
     * 查询所有销售机会，可是SaleChance的任意属性
     * @param saleChance
     * @param page
     * @param rows
     * @param request
     * @return
     */
    @PostMapping("find-all-sale-chance")
    @ResponseBody
    public Map queryAllSaleChance(SaleChance saleChance, int page, int rows, HttpServletRequest request){
        List<SaleChance> list = saleChanceService.advanceSaleChance(saleChance,page,rows,null,
                null,null,null,null,null,null,null,null,null);


        Long total = saleChanceService.advanceSaleChanceCount(saleChance,null,null,
                null,null,null,null,null,null,null,null);

        Map map = new HashMap();
        map.put("rows",list);
        map.put("total",total);
        return map;
    }

    /**
     * 高级查询 销售机会
     * 可以是SaleChance的任意属性
     * @param saleChance
     * @param page
     * @param rows
     * @param expectStartTime
     * @param expectEndTime
     * @param createStartTime
     * @param createEndTime
     * @param updateStartTime
     * @param updateEndTime
     * @return
     */
    @PostMapping("advance-sale-chance")
    @ResponseBody
    public Map queryAllSaleChance(SaleChance saleChance, int page, int rows, BigDecimal minDay, BigDecimal maxDay,
                                  BigDecimal minMoney, BigDecimal maxMoney, Date expectStartTime, Date expectEndTime, Date createStartTime, Date createEndTime,
                                  Date updateStartTime, Date updateEndTime){
        Long total = saleChanceService.advanceSaleChanceCount(saleChance,minDay,maxDay,minMoney,maxMoney,expectStartTime,expectEndTime,createStartTime,
                createEndTime,updateStartTime,updateEndTime);
        List<SaleChance> list = saleChanceService.advanceSaleChance(saleChance,page,rows,minDay,maxDay,minMoney,maxMoney,expectStartTime,expectEndTime,createStartTime,
                createEndTime,updateStartTime,updateEndTime);
        Map map = new HashMap();
        map.put("total",total);
        map.put("rows",list);
        return map;


    }

    /**
     * 根据客户id查找客户名称
     * @param clientId
     * @return
     */
    @PostMapping("get-client-name")
    @ResponseBody
    public  String getClientName(Long clientId){
        return saleChanceService.getClientName(clientId);
    }

    /**
     * 根据客户id查找客户名称
     * @param contactId
     * @return
     */
    @PostMapping("get-contact-name")
    @ResponseBody
    public  String getContactName(Long contactId){
        return saleChanceService.getContactName(contactId);
    }

    /**
     * 添加一个销售机会
     * @param asterisk
     * @param chanceTheme
     * @param clientId
     * @param contactId
     * @param userId
     * @param expectDate
     * @param expectMoney
     * @param findTime
     * @param intentRpoduct
     * @param outlineNeed
     * @param possibility
     * @param priority
     * @param remarks
     * @param stage
     * @param stageRemain
     * @param state
     * @param succedaneum
     * @param type
     * @param updateDate
     * @return
     */
    @PostMapping("zjk-add-saleChance")
    @ResponseBody
    public Result addSaleChance(String asterisk,String chanceTheme,BigDecimal clientId,BigDecimal contactId,BigDecimal userId,
                                Date expectDate,BigDecimal expectMoney,Date findTime,String intentRpoduct,
                                String outlineNeed,String possibility,String priority,String remarks,String stage,
                                BigDecimal stageRemain,String state, String succedaneum,String type,Date updateDate){
        try {
            Result result = saleChanceService.addSaleChance(asterisk,chanceTheme,clientId,contactId,userId,expectDate,expectMoney,findTime,intentRpoduct,
                    outlineNeed,possibility,priority,remarks,stage,stageRemain,state,succedaneum,type,updateDate);
            return  result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(e);
        }

    }

    /**
     * 编辑销售机会信息
     * @return
     */
    @PostMapping("zjk-edit-saleChance")
    @ResponseBody
    public Result editSaleChance(SaleChance saleChance){
        Result result = saleChanceService.editSaleChance(saleChance);
        return result;
    }

    /**
     * 获取所有销售机会主题
     * @return
     */
    @PostMapping("getSaleChanceTheme")
    @ResponseBody
    public List<SaleChance> getSaleChanceTheme(){
        return saleChanceService.getSaleChanceTheme();
    }

    /**
     * 根据id获取一个机会主题
     * @param chId
     * @return
     */
    @PostMapping("getSaleChanceThemeById")
    @ResponseBody
    public String getSaleChanceThemeById(Long chId){
        return  saleChanceService.getSaleChanceThemeById(chId);
    }


    /**
     * 下载
     * @param request
     * @param response
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/saleChance-export-excel")
    public void exportExcelSaleChance(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        List<SaleChance> saleChanceList = saleChanceService.findAll();
        ExportExcel ee= new ExportExcel();
        String[] headers = { "ID", "星标", "机会主题", "客户名称", "联系人", "预计签单日期", "预计金额", "发现时间", "意向产品", "需求概要", "可能性", "优先级"
                ,  "备注", "是否删除", "阶段", "阶段停留", "状态", "替代品威胁", "类型", "更新日期", "用户"};

        String fileName = "销售机会表";
        ee.exportExcel(headers,saleChanceList,fileName,response);
    }

    /**
     * 通过excel导入销售机会
     * @param file
     * @param uploadWay
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/upload-excel-saleChance")
    @ResponseBody
    public Result uploadExcelSaleChance(@RequestParam(value="file",required = false) MultipartFile file, String uploadWay, HttpServletRequest request, HttpServletResponse response){
        return saleChanceService.uploadExcelSaleChance(file, uploadWay);
    }

}
